package com.telit.firecommand.controller.pub;

import cn.hutool.core.util.StrUtil;
import com.telit.common.constant.RoleCodeConstants;
import com.telit.common.entity.DataReturn;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.util.RedisUtils;
import com.telit.firecommand.constant.RedisKeyConstant;
import com.telit.firecommand.dto.GeoCunDTO;
import com.telit.firecommand.entity.CommFireAlarm;
import com.telit.firecommand.facade.FireTriggerFacadeService;
import com.telit.firecommand.service.FireAlarmsService;
import com.telit.firecommand.service.IGeoXzqhCunService;
import io.netty.util.internal.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 火情开放API
 *
 * @author yuezhuang
 * @date 2022-10-18
 *         update by chencf on 2022-12-08
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/pub/fire-alarms", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "火情相关开放API", value = "火情相关开放API", produces = MediaType.APPLICATION_JSON_VALUE)
public class FireAlarmsPubController {

    private final FireAlarmsService fireAlarmsService;

    private final FireTriggerFacadeService fireTriggerFacadeService;

    private final IGeoXzqhCunService geoXzqhCunService;


    @PostMapping("/addFireByCamera")
    @ApiOperation("接受监控推送火情")
    public boolean addFireByCamera(@RequestBody String data) throws Exception {
        log.info("receive camera fire data: " + data);
        fireAlarmsService.addFireByCamera(data);
//        try {
//            Thread.sleep(1000L);
//            fireAlarmsService.addFireByCamera(data);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//            return false;
//        }
        return true;
    }

    @PostMapping(value = "/addFireInfoByApp", produces = "application/json;charset=UTF-8")
    @ApiOperation("护林员和飞手app端调用上报火情 护林员上报resource传1 飞手上报resource传2")
    public DataReturn addFireInfoByApp(@RequestBody CommFireAlarm commFireAlarm, HttpServletRequest httpServletRequest)
            throws Exception {
        GeoCunDTO districtByPoint = geoXzqhCunService.getDistrictByPoint(commFireAlarm.getLongitude(),
                commFireAlarm.getLatitude());
        if (districtByPoint == null) {
            throw new ForestBaseException("经纬度不在宜昌市范围内！");
        }
        commFireAlarm.setDistrictCode(districtByPoint.getXiang());
        commFireAlarm.setDistrictName(districtByPoint.getXiangName());
        commFireAlarm.setFinderId(RoleCodeConstants.USER_ID_WECHAT);
        if (StrUtil.isEmpty(commFireAlarm.getFinderName())) {
            commFireAlarm.setFinderName(RoleCodeConstants.USER_NAME_WECHAT);
        }
        commFireAlarm.setFindTime(new Date());
        commFireAlarm.setStatus("1");
        commFireAlarm.setAction("1");
        fireTriggerFacadeService.addFire(commFireAlarm, httpServletRequest);
        return DataReturn.ok();
    }

    @GetMapping("/filterSwitch")
    @ApiOperation("过滤开发，flag:0关闭过滤，1开启过滤;isSearch:1代表进行查询，")
    public DataReturn filterSwitch(@RequestParam(required = false) String flag,
                                   @RequestParam(required = false) String isSearch)throws Exception {
        log.info("filterSwitch flag: " + flag);
        log.info("filterSwitch isSearch: " + isSearch);
        if(StrUtil.isNotEmpty(isSearch)){
            return DataReturn.ok(RedisUtils.get(RedisKeyConstant.FILTER_SWITCH));
        }
        RedisUtils.set(RedisKeyConstant.FILTER_SWITCH, flag);
        return DataReturn.ok();
    }

//    @GetMapping("/test")
//    @ApiOperation("test")
//    public boolean test(String id) throws Exception {
//        fireAlarmsService.download(id);
//        return true;
//    }

//
//    /**
//     * 火情卫星上报
//     *
//     * @param addFireBySatelliteRequest FireAlarmsInsertRequest
//     *
//     * @return DataReturn
//     */
//    @Log(service = ServiceName.FIRE_ALARMS, businessType = BusinessType.INSERT, operatorType = OperatorType.OTHER)
//    @PostMapping(path = "/save-by-satellite")
//    public DataReturn<FireAlarmsDTO> saveBySatellite(@RequestBody AddFireBySatelliteRequest addFireBySatelliteRequest) {
//        return DataReturn.ok(fireAlarmsService.saveBySatellite(addFireBySatelliteRequest));
//    }
//
//
//    //修改火情状态
//    @Log(service = ServiceName.FIRE_ALARMS, businessType = BusinessType.BUSINESS_LOG, operatorType = OperatorType.OTHER)
//    @ApiOperation(value = "修改火情状态")
//    @PostMapping(path = "/fireHandle")
//    public DataReturn fireHandle(@RequestBody FireAlarmsUpdateStatusPubDTO fireAlarmsUpdateStatusPubDTO) {
//        //确认火情  确认非火情
//         if (fireAlarmsUpdateStatusPubDTO.getIsTrue()==1||fireAlarmsUpdateStatusPubDTO.getIsTrue()==2){
//            ConfirmAlarmDTO confirmAlarmDTO = new ConfirmAlarmDTO();
//            confirmAlarmDTO.setId(fireAlarmsUpdateStatusPubDTO.getFireId());
//            confirmAlarmDTO.setOldStatus(fireAlarmsUpdateStatusPubDTO.getOldStatus());
//            confirmAlarmDTO.setUserCompany(fireAlarmsUpdateStatusPubDTO.getUserCompany());
//            confirmAlarmDTO.setUserName(fireAlarmsUpdateStatusPubDTO.getUserName());
//            confirmAlarmDTO.setIsTrue(fireAlarmsUpdateStatusPubDTO.getIsTrue());
//            confirmAlarmDTO.setFireId(fireAlarmsUpdateStatusPubDTO.getFireId());
//            fireAlarmsService.confirmAlarm(confirmAlarmDTO);
//        }else {
//             //接警操作    完成处置操作
//             FireAlarmsUpdateStatusDTO fireAlarmsUpdateStatusDTO = new FireAlarmsUpdateStatusDTO();
//             fireAlarmsUpdateStatusDTO.setId(fireAlarmsUpdateStatusPubDTO.getFireId());
//             fireAlarmsUpdateStatusDTO.setOldStatus(fireAlarmsUpdateStatusPubDTO.getOldStatus());
//             fireAlarmsUpdateStatusDTO.setUserCompany(fireAlarmsUpdateStatusPubDTO.getUserCompany());
//             fireAlarmsUpdateStatusDTO.setUserName(fireAlarmsUpdateStatusPubDTO.getUserName());
//             fireAlarmsUpdateStatusDTO.setStatus(fireAlarmsUpdateStatusPubDTO.getStatus());
//             fireAlarmsUpdateStatusDTO.setFireId(fireAlarmsUpdateStatusPubDTO.getFireId());
//             fireAlarmsService.updateStatus(fireAlarmsUpdateStatusDTO);
//         }
//        return DataReturn.ok();
//    }
//
//
//
}
//
//
//
//
//
//
//
//
//
//
//
//
//
